if (window.basicoperations == undefined) {

    window.basicopertaions = {};

    function mergeObject(object, xhr, done) {
        for (prop in object) { if (object.hasOwnProperty(prop)) { delete object[prop]; } }
        $.extend(true, object, xhr);
        if (done != null) {

            done(object);
        }
    }

    function Message(object) {

        $.extend(this, object);
    }

    Message.prototype.restLoad = function (url, success, failure) {

        var self = this;
        $.ajax({
            headers: {
                Accept: "application/json",
                "Content-Type": "application/json"
            },
            type: 'GET',
            url: (typeof url == "string")? url:url.URL,
            success: function (xhr) {

                mergeObject(self, xhr, success);

            },
            cache: false,
            error: failure
        })
    };

    Message.prototype.restReload = function (url, success, failure) {

        $.ajax({
            headers: {
                Accept : "application/json",
                "Content-Type": "application/json"
            },
            type: 'GET',
            url: this.restLink("self").URL,
            success:function (xhr) {

                mergeObject(self, xhr, success);

            },
            cache: false,
            error: failure
        })
    };

    Message.prototype.restCreate = function (url, success, failure) {

        var self = this;
        $.ajax({
            headers: {
                Accept : "application/json",
                "Content-Type": "application/json"
            },
            type: 'POST',
            data: $.toJSON(self),
            url: url.URL,
            success: function(xhr) {

                mergeObject(self, xhr, success);
            },
            error: failure
        })
    }

    Message.prototype.restUpdate = function(name) {

        var self = this;
        $.ajax({
            headers: {
                Accept : "application/json",
                "Content-Type": "application/json"
            },
            type: 'PUT',
            data: $.toJSON(self),
            url:  this.restLink("self").URL,
            success: success,
            error: failure
        })    
    };
    
    Message.prototype.restPut = function(url, success, failure) {

        var self = this;
        $.ajax({
            headers: {
                Accept : "application/json",
                "Content-Type": "application/json"
            },
            type: 'PUT',
            data: $.toJSON(self),
            url:  url.URL,
            success: function(xhr) {

                mergeObject(self, xhr, success);
            },
            error: failure
        })
    }
    
    Message.prototype.restRemove = function(success, failure) {

        $.ajax({
            headers: {
                Accept : "application/json",
                "Content-Type": "application/json"
            },
            type: 'DELETE',
            url:  this.restLink("self").location,
            success: success,
            error: failure
        })
    }
    
    
    
    
    
    Message.prototype.restLink = function(name) {

            for(var i=0;i<this.links.length;i++)
            {
                var link = this.links[i];
                if(name == link.name){
                    return link;
                }
            }

            throw "structure has no links";
    };
}